<?php

namespace Lib\Bootstrap;

use Controller\IndexController;
use Controller\ErroController;
use Exception;
use Lib\View\View;
use Lib\Renderer\TwigFactory;

/**
 * Classe resposnsavel por inciar a aplicação
 *
 * url[0] - responsavel por definir o controller
 * url[1] - responsavel por definir a action do controller
 * url[2] - responsavel por definir o valor do action no controller
 */
class Bootstrap {
    
    protected $erroController = null;

    public function __construct()
    {                
        $url = isset($_GET["url"]) ? $_GET["url"] : null;
        $url = explode('/',rtrim($url,'/'));
        
        if (empty($url[0])) {
            
            $controller = new IndexController();
            $this->render(
                $controller->indexAction()
            );
            return false;
        }
        
        $url[0] = $this->routeToController($url[0]);

        $url[0] = '\Controller\\'.$url[0];
        
        
        if (!class_exists($url[0])) {
            $this->render(
                $this->getErroController()->indexAction()
            );
            return;
        }
        
        $controller = new $url[0];
        
        if (isset($url[2])) {
            $url[1] = $this->routeToController($url[1], 'Action');
            
            if (method_exists($controller, $url[1])) {
                $this->render(
                    $controller->$url[1]($url[2])
                );
            } else {
                $this->render(
                    $this->getErroController()->indexAction()
                );
            }

            return;
        }
        
        if(isset($url[1])) {
            $url[1] = $this->routeToController($url[1], 'Action');

            if (method_exists($controller, $url[1])) {
                $this->render(
                    $controller->$url[1]()
                );
            } else {
                $this->render(
                    $this->getErroController()->indexAction()
                );
            }

            return;
        }
        
        $this->render(
            $controller->indexAction()
        );
    }

    /**
     * classe responsavel por converter rota em controller
     * @param string $url
     * return string
     */
    private function routeToController($url, $type = 'Controller')
    {
        if (strstr($url, '-') === false) {
            return ucfirst($url) . $type;
        }   

        $parts = explode('-', $url);
        $newUrl = null;

        foreach ($parts as $part) {
            $newUrl .= ucfirst($part);      
        }

        return $newUrl . $type;
    }

    protected function getErroController()
    {
        if ($this->erroController == null) {
            $this->erroController = new ErroController();
        }

        return $this->erroController;
    }

    protected function render($view)
    {
        if($view instanceof View) {
            if ($view->getRenderer() === null) {
                $view->setRenderer(
                    TwigFactory::factory()
                );
            }

            $view->render();   
        }
    }
}

